package org.jeecg.modules.fw.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.fw.entity.FwRoomCleanUseConsumable;
import org.jeecg.modules.fw.entity.FwRoomConsumable;
import org.jeecg.modules.fw.entity.FwRoomLayoutInConsumable;
import org.jeecg.modules.fw.service.IFwRoomConsumableService;
import org.jeecg.modules.fw.service.IFwRoomLayoutInConsumableService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: fw_room_layout_in_consumable
 * @Author: jeecg-boot
 * @Date:   2023-05-25
 * @Version: V1.0
 */
@Api(tags="fw_room_layout_in_consumable")
@RestController
@RequestMapping("/fw/fwRoomLayoutInConsumable")
@Slf4j
public class FwRoomLayoutInConsumableController extends JeecgController<FwRoomLayoutInConsumable, IFwRoomLayoutInConsumableService> {
	@Autowired
	private IFwRoomLayoutInConsumableService fwRoomLayoutInConsumableService;
	@Resource
	private IFwRoomConsumableService fwRoomConsumableService;

	/**
	 * 分页列表查询
	 *
	 * @param fwRoomLayoutInConsumable
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "fw_room_layout_in_consumable-分页列表查询")
	@ApiOperation(value="fw_room_layout_in_consumable-分页列表查询", notes="fw_room_layout_in_consumable-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FwRoomLayoutInConsumable>> queryPageList(FwRoomLayoutInConsumable fwRoomLayoutInConsumable,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FwRoomLayoutInConsumable> queryWrapper = QueryGenerator.initQueryWrapper(fwRoomLayoutInConsumable, req.getParameterMap());
		Page<FwRoomLayoutInConsumable> page = new Page<FwRoomLayoutInConsumable>(pageNo, pageSize);
		IPage<FwRoomLayoutInConsumable> pageList = fwRoomLayoutInConsumableService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 根据房型查询配置易耗品列表
	  * @param type 类型 1续房 2退房
	  * @param roomLayoutId 房型id
	  * @param hotelId
	  * @return
	  */
	 @ApiOperation(value="根据房型查询配置易耗品列表")
	 @GetMapping(value = "/layoutInConsumableList")
	 public Result<List<FwRoomLayoutInConsumable>> layoutInConsumableList(@RequestParam(name="type") Integer type,
																  @RequestParam(name="roomLayoutId") String roomLayoutId,@RequestParam(name="hotelId") String hotelId) {
		 LambdaQueryWrapper<FwRoomLayoutInConsumable> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(FwRoomLayoutInConsumable::getHotelId, hotelId);
		 queryWrapper.eq(FwRoomLayoutInConsumable::getType, type);
		 queryWrapper.eq(FwRoomLayoutInConsumable::getRoomLayoutId, roomLayoutId);
		 List<FwRoomLayoutInConsumable> fwRoomLayoutInConsumableList = fwRoomLayoutInConsumableService.list(queryWrapper);

		 LambdaQueryWrapper<FwRoomConsumable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		 lambdaQueryWrapper.eq(FwRoomConsumable::getHotelId, hotelId);
		 List<FwRoomConsumable> fwRoomConsumables = fwRoomConsumableService.list(lambdaQueryWrapper);
		 List<FwRoomLayoutInConsumable> list = new ArrayList<>();
		 fwRoomConsumables.forEach(t -> {
			 FwRoomLayoutInConsumable roomLayoutInConsumable = new FwRoomLayoutInConsumable();
			 roomLayoutInConsumable.setRoomConsumableId(t.getId());
			 roomLayoutInConsumable.setType(t.getType());
			 roomLayoutInConsumable.setGoodName(t.getGoodName());
			 roomLayoutInConsumable.setNum(0);
			 Optional<FwRoomLayoutInConsumable> optional = fwRoomLayoutInConsumableList.stream().filter(f -> f.getRoomConsumableId().equals(t.getId()) && f.getHotelId().equals(t.getHotelId())&&f.getRoomLayoutId().equals(roomLayoutId)).findFirst();
			 if (optional.isPresent()) {
				 roomLayoutInConsumable.setNum(optional.get().getNum());
			 }
			 list.add(roomLayoutInConsumable);
		 });
		 return Result.OK(list);
	 }

	 /**
	  * 根据房型保存配置易耗品
	  * @param fwRoomLayoutInConsumableList
	  * @return
	  */
	 @ApiOperation(value="根据房型保存配置易耗品")
	 @PostMapping(value = "/layoutInConsumableSave")
	 public Result<String> layoutInConsumableSave(@RequestBody List<FwRoomLayoutInConsumable> fwRoomLayoutInConsumableList,@RequestParam(name="hotelId") String hotelId) {
		 List<FwRoomLayoutInConsumable> list = new ArrayList<>();
		 fwRoomLayoutInConsumableList.forEach(t -> {
			 if (t.getNum() > 0) {
				 t.setHotelId(hotelId);
				 t.setTenantId(TokenUtils.currentTenantId());
				 list.add(t);
			 }
		 });
		 LambdaQueryWrapper<FwRoomLayoutInConsumable> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		 lambdaQueryWrapper.eq(FwRoomLayoutInConsumable::getRoomLayoutId,fwRoomLayoutInConsumableList.get(0).getRoomLayoutId());
		 fwRoomLayoutInConsumableService.remove(lambdaQueryWrapper);
		 fwRoomLayoutInConsumableService.saveBatch(list);
		 return Result.OK("添加成功！");
	 }

	/**
	 *   添加
	 *
	 * @param fwRoomLayoutInConsumable
	 * @return
	 */
	@AutoLog(value = "fw_room_layout_in_consumable-添加")
	@ApiOperation(value="fw_room_layout_in_consumable-添加", notes="fw_room_layout_in_consumable-添加")
	//@RequiresPermissions("fw:fw_room_layout_in_consumable:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody FwRoomLayoutInConsumable fwRoomLayoutInConsumable) {
		fwRoomLayoutInConsumableService.save(fwRoomLayoutInConsumable);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param fwRoomLayoutInConsumable
	 * @return
	 */
	@AutoLog(value = "fw_room_layout_in_consumable-编辑")
	@ApiOperation(value="fw_room_layout_in_consumable-编辑", notes="fw_room_layout_in_consumable-编辑")
	//@RequiresPermissions("fw:fw_room_layout_in_consumable:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody FwRoomLayoutInConsumable fwRoomLayoutInConsumable) {
		fwRoomLayoutInConsumableService.updateById(fwRoomLayoutInConsumable);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "fw_room_layout_in_consumable-通过id删除")
	@ApiOperation(value="fw_room_layout_in_consumable-通过id删除", notes="fw_room_layout_in_consumable-通过id删除")
	//@RequiresPermissions("fw:fw_room_layout_in_consumable:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		fwRoomLayoutInConsumableService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "fw_room_layout_in_consumable-批量删除")
	@ApiOperation(value="fw_room_layout_in_consumable-批量删除", notes="fw_room_layout_in_consumable-批量删除")
	//@RequiresPermissions("fw:fw_room_layout_in_consumable:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fwRoomLayoutInConsumableService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "fw_room_layout_in_consumable-通过id查询")
	@ApiOperation(value="fw_room_layout_in_consumable-通过id查询", notes="fw_room_layout_in_consumable-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<FwRoomLayoutInConsumable> queryById(@RequestParam(name="id",required=true) String id) {
		FwRoomLayoutInConsumable fwRoomLayoutInConsumable = fwRoomLayoutInConsumableService.getById(id);
		if(fwRoomLayoutInConsumable==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(fwRoomLayoutInConsumable);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param fwRoomLayoutInConsumable
    */
    //@RequiresPermissions("fw:fw_room_layout_in_consumable:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FwRoomLayoutInConsumable fwRoomLayoutInConsumable) {
        return super.exportXls(request, fwRoomLayoutInConsumable, FwRoomLayoutInConsumable.class, "fw_room_layout_in_consumable");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("fw:fw_room_layout_in_consumable:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FwRoomLayoutInConsumable.class);
    }

}
